package
{
	import Components.MovementComponent;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	public class ResourceNode
	{
		public var movementComponent:MovementComponent = new MovementComponent;
		public var timeSpentInside:Number = 1;
		public var maxInside:int = 2;
		public var type:int;
		public var display:Sprite = new Sprite();
		
		private var m_gatherersInside:Vector.<ResourceGatherer> = new Vector.<ResourceGatherer>;
		private var m_gathererByTimer:Dictionary = new Dictionary(true);
		
		public function ResourceNode()
		{
			var bg:Sprite = VTUtil.getRectOutline(8,8,0xffff00)
			bg.x = -bg.width/2;
			bg.y = -bg.height/2
			display.addChild(bg);
		}
		
		public function update(delta:int):void{
			display.x = movementComponent.location.x * rts1.RATIO.x;
			display.y = movementComponent.location.y * rts1.RATIO.y;			
		}
		
		public function canEnter():Boolean{
			return (m_gatherersInside.length < maxInside);
		}
		
		public function enter(resourceGatherer:ResourceGatherer):void{
			resourceGatherer.display.visible = false;
			m_gatherersInside.push(resourceGatherer);
			var timer:Timer = new Timer(timeSpentInside*1000,1);
			timer.addEventListener(TimerEvent.TIMER, onTimerTick);
			timer.start();
			m_gathererByTimer[timer] = resourceGatherer; 
		}
		
		private function onTimerTick(te:TimerEvent):void{
			var timer:Timer = te.currentTarget as Timer;
			var gatherer:ResourceGatherer = m_gathererByTimer[timer];
			gatherer.resourceAmount = 50;
			gatherer.display.visible = true;
			m_gatherersInside.splice(m_gatherersInside.indexOf(gatherer),1);
			delete m_gathererByTimer[timer];
		}
		
		public function isInside(resourceGatherer:ResourceGatherer):Boolean{
			return (m_gatherersInside.indexOf(resourceGatherer) >= 0);
		}
	}
}